package com.tongzhan.service;

import com.tongzhan.base.BaseCriteria;
import com.tongzhan.entity.SysDict;
import com.tongzhan.entity.party.ActionCenter;
import com.tongzhan.entity.qiaotai.*;
import com.tongzhan.entity.xiangxian.ExamplePoint;
import com.tongzhan.repository.*;
import com.tongzhan.repository.party.PeopleOpinionRepository;
import com.tongzhan.repository.qiaotai.*;
import com.tongzhan.util.DateUtil;
import com.tongzhan.utils.DateSwitcher;
import com.tongzhan.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.temporal.TemporalAdjusters;
import java.util.*;

@Service
public class QiaotaiService {

    @Autowired
    public ExamplePointRepository examplePointRepository;
    @Autowired
    private OpinionCarRepository opinionCarRepository;
    @Autowired
    private TPersonnelRepository tPersonnelRepository;
    @Autowired
    private ActionCenterRepository actionCenterRepository;
    @Autowired
    private SysDictRepository sysDictRepository;
    @Autowired
    private PeopleOpinionRepository peopleOpinionRepository;
    @Autowired
    private TownInfoRepository townInfoRepository;
    @Autowired
    private BasicDemoRepository basicDemoRepository;
    @Autowired
    public TPersonnelRepository personnelRepository;
    @Autowired
    private OverseasCompanyRepository overseasCompanyRepository;

    public List<BasicDemo> getMapList(Integer type, String date) {
        if (type == null) {
            type = 1;
        }
        if (date == null || "".equals(date)) {
            date = DateUtil.getSysYear();
        }
        String startDate = DateUtil.getStartDate(type, date);
        String endDate = DateUtil.getEndDate(type, date);
        BaseCriteria criteria = DateUtil.getDateSearchCriteria(startDate, endDate, "time");

        List<BasicDemo> list = basicDemoRepository.findAll(new Query().addCriteria(BaseCriteria.where("is_del").is(0).andOperator(criteria)));
        return list;
    }

    public List<OpinionCar> getOpinionCarList(Integer type, String date) {
        if (type == null) {
            type = 1;
        }
        if (date == null || "".equals(date)) {
            date = DateUtil.getSysYear();
        }
        String startDate = DateUtil.getStartDate(type, date);
        String endDate = DateUtil.getEndDate(type, date);
        BaseCriteria criteria = DateUtil.getDateSearchCriteria(startDate, endDate, "time");
        Query query = new Query();
        List<OpinionCar> list = opinionCarRepository.find(query.addCriteria(Criteria.where("is_del").is(0).andOperator(criteria)).with(Sort.by(Sort.Direction.DESC, "time")), null);

        return list;
    }

    public Map<String, Object> getUserInfoList(Integer type, String date) {
        Map<String, Object> map = new HashMap<>();
        List<Long> countList = new ArrayList<>();
        if (type == null) {
            type = 1;
        }
        if (date == null || "".equals(date)) {
            date = DateUtil.getSysYear();
        }
        String startDate = DateUtil.getStartDate(type, date);
        String endDate = DateUtil.getEndDate(type, date);
        BaseCriteria criteria = DateUtil.getDateSearchCriteria(startDate, endDate, "addTime");
        List<Map<String, Object>> userDataList = new ArrayList<>();
        //查城镇数据
        List<TownInfo> allOne = townInfoRepository.findAll(new Query().addCriteria(BaseCriteria.where("is_del").is(0)));
        allOne.forEach(one -> {
            List<UserInfoVO> list = new ArrayList<>();
            // typeId 人员类型id(1:留学生、2:华人华侨、3:港澳台同胞)
            List<SysDict> all = sysDictRepository.findAll(new Query().addCriteria(Criteria.where("isDelete").is(0).and("pid").is("486536192998125568")));
            all.forEach(c -> {
                Criteria baseCriteria = BaseCriteria.where("townTypeId").is(one.getId()).and("is_del").is(0).and("type").is(Integer.valueOf(c.getType()));
//            baseCriteria.and("type").is(Integer.valueOf(c.getType()));

                long count = tPersonnelRepository.count(new Query(baseCriteria.andOperator(criteria)));
                countList.add(count);
                UserInfoVO userInfoVO = new UserInfoVO();
                userInfoVO.setName(c.getValue());
                userInfoVO.setCount(count);
                list.add(userInfoVO);
            });
            userDataList.add(new HashMap<String, Object>() {{
                put("town", one.getName());
                put("list", list);
            }});
        });


        long totalCount = 0;
        for (Long l : countList) {
            totalCount = totalCount + l;
        }
        map.put("totalCount", totalCount);
        map.put("dataList", userDataList);
        return map;
    }

    public Map<String, Object> getPersonnelList(Integer type, String date, String name, Integer typeId) {
        Map<String, Object> map = new HashMap<>();
        if (type == null) {
            type = 1;
        }
        if (date == null || "".equals(date)) {
            date = DateUtil.getSysYear();
        }
        String startDate = DateUtil.getStartDate(type, date);
        String endDate = DateUtil.getEndDate(type, date);
        BaseCriteria criteria = DateUtil.getDateSearchCriteria(startDate, endDate, "addTime");

        Criteria criteria2 = BaseCriteria.where("is_del").is(0);
        if (name != null && !"".equals(name)) {
            criteria2.andOperator(BaseCriteria.where("name").like(name));
        }
        if (typeId != null) {
            criteria2.and("typeId").is(typeId);
        }

        List<Personnel> list = tPersonnelRepository.find(new Query().addCriteria(criteria2).addCriteria(criteria), null);
        map.put("dataList", list);
        return map;
    }

    public Map<String, Object> getActionCenterList(Integer type, String date) {
        if (type == null) {
            type = 1;
        }
        if (date == null || "".equals(date)) {
            date = DateUtil.getSysYear();
        }
        String year = date.substring(0, 4);
        String month1Start = year + "-01-01";
        String month2Start = year + "-02-01";
        String month3Start = year + "-03-01";
        String month4Start = year + "-04-01";
        String month5Start = year + "-05-01";
        String month6Start = year + "-06-01";
        String month7Start = year + "-07-01";
        String month8Start = year + "-08-01";
        String month9Start = year + "-09-01";
        String month10Start = year + "-10-01";
        String month11Start = year + "-11-01";
        String month12Start = year + "-12-01";

        String month1End = year + "-01-31";
        String month2End = year + "-02-29";
        String month3End = year + "-03-31";
        String month4End = year + "-04-30";
        String month5End = year + "-05-31";
        String month6End = year + "-06-30";
        String month7End = year + "-07-31";
        String month8End = year + "-08-31";
        String month9End = year + "-09-30";
        String month10End = year + "-10-31";
        String month11End = year + "-11-30";
        String month12End = year + "-12-31";
        Map<String, Object> map = new HashMap<>();
        List<String> dateList = new ArrayList<>();
        List<Long> countList = new ArrayList<>();
        dateList.add("1月");
        dateList.add("2月");
        dateList.add("3月");
        dateList.add("4月");
        dateList.add("5月");
        dateList.add("6月");
        dateList.add("7月");
        dateList.add("8月");
        dateList.add("9月");
        dateList.add("10月");
        dateList.add("11月");
        dateList.add("12月");

        BaseCriteria criteria1 = DateUtil.getDateSearchCriteria(month1Start, month1End, "time");
        BaseCriteria criteria2 = DateUtil.getDateSearchCriteria(month2Start, month2End, "time");
        BaseCriteria criteria3 = DateUtil.getDateSearchCriteria(month3Start, month3End, "time");
        BaseCriteria criteria4 = DateUtil.getDateSearchCriteria(month4Start, month4End, "time");
        BaseCriteria criteria5 = DateUtil.getDateSearchCriteria(month5Start, month5End, "time");
        BaseCriteria criteria6 = DateUtil.getDateSearchCriteria(month6Start, month6End, "time");
        BaseCriteria criteria7 = DateUtil.getDateSearchCriteria(month7Start, month7End, "time");
        BaseCriteria criteria8 = DateUtil.getDateSearchCriteria(month8Start, month8End, "time");
        BaseCriteria criteria9 = DateUtil.getDateSearchCriteria(month9Start, month9End, "time");
        BaseCriteria criteria10 = DateUtil.getDateSearchCriteria(month10Start, month10End, "time");
        BaseCriteria criteria11 = DateUtil.getDateSearchCriteria(month11Start, month11End, "time");
        BaseCriteria criteria12 = DateUtil.getDateSearchCriteria(month12Start, month12End, "time");
        // type 类型 1是党派 2是侨台 3是乡贤
        long count1 = actionCenterRepository.count(new Query(Criteria.where("is_del").is(0)).addCriteria(BaseCriteria.where("type").is(2).andOperator(criteria1)));
        long count2 = actionCenterRepository.count(new Query(Criteria.where("is_del").is(0)).addCriteria(BaseCriteria.where("type").is(2).andOperator(criteria2)));
        long count3 = actionCenterRepository.count(new Query(Criteria.where("is_del").is(0)).addCriteria(BaseCriteria.where("type").is(2).andOperator(criteria3)));
        long count4 = actionCenterRepository.count(new Query(Criteria.where("is_del").is(0)).addCriteria(BaseCriteria.where("type").is(2).andOperator(criteria4)));
        long count5 = actionCenterRepository.count(new Query(Criteria.where("is_del").is(0)).addCriteria(BaseCriteria.where("type").is(2).andOperator(criteria5)));
        long count6 = actionCenterRepository.count(new Query(Criteria.where("is_del").is(0)).addCriteria(BaseCriteria.where("type").is(2).andOperator(criteria6)));
        long count7 = actionCenterRepository.count(new Query(Criteria.where("is_del").is(0)).addCriteria(BaseCriteria.where("type").is(2).andOperator(criteria7)));
        long count8 = actionCenterRepository.count(new Query(Criteria.where("is_del").is(0)).addCriteria(BaseCriteria.where("type").is(2).andOperator(criteria8)));
        long count9 = actionCenterRepository.count(new Query(Criteria.where("is_del").is(0)).addCriteria(BaseCriteria.where("type").is(2).andOperator(criteria9)));
        long count10 = actionCenterRepository.count(new Query(Criteria.where("is_del").is(0)).addCriteria(BaseCriteria.where("type").is(2).andOperator(criteria10)));
        long count11 = actionCenterRepository.count(new Query(Criteria.where("is_del").is(0)).addCriteria(BaseCriteria.where("type").is(2).andOperator(criteria11)));
        long count12 = actionCenterRepository.count(new Query(Criteria.where("is_del").is(0)).addCriteria(BaseCriteria.where("type").is(2).andOperator(criteria12)));
        countList.add(count1);
        countList.add(count2);
        countList.add(count3);
        countList.add(count4);
        countList.add(count5);
        countList.add(count6);
        countList.add(count7);
        countList.add(count8);
        countList.add(count9);
        countList.add(count10);
        countList.add(count11);
        countList.add(count12);
        long totalCount = count1 + count2 + count3 + count4 + count5 + count6 + count7 + count8 + count9 + count10 + count11 + count12;
        map.put("totalCount", totalCount);
        map.put("countList", countList);
        map.put("dateList", dateList);
        return map;
    }


    public Map<String, Object> getPeopleOpinionCount(Integer type, String date) {
        if (type == null) {
            type = 1;
        }
        if (date == null || "".equals(date)) {
            date = DateUtil.getSysYear();
        }
        String year = date.substring(0, 4);
        String month1Start = year + "-01-01";
        String month2Start = year + "-02-01";
        String month3Start = year + "-03-01";
        String month4Start = year + "-04-01";
        String month5Start = year + "-05-01";
        String month6Start = year + "-06-01";
        String month7Start = year + "-07-01";
        String month8Start = year + "-08-01";
        String month9Start = year + "-09-01";
        String month10Start = year + "-10-01";
        String month11Start = year + "-11-01";
        String month12Start = year + "-12-01";

        String month1End = year + "-01-31";
        String month2End = year + "-02-29";
        String month3End = year + "-03-31";
        String month4End = year + "-04-30";
        String month5End = year + "-05-31";
        String month6End = year + "-06-30";
        String month7End = year + "-07-31";
        String month8End = year + "-08-31";
        String month9End = year + "-09-30";
        String month10End = year + "-10-31";
        String month11End = year + "-11-30";
        String month12End = year + "-12-31";
        Map<String, Object> map = new HashMap<>();
        List<String> dateList = new ArrayList<>();
        List<Long> countList = new ArrayList<>();
        dateList.add("1月");
        dateList.add("2月");
        dateList.add("3月");
        dateList.add("4月");
        dateList.add("5月");
        dateList.add("6月");
        dateList.add("7月");
        dateList.add("8月");
        dateList.add("9月");
        dateList.add("10月");
        dateList.add("11月");
        dateList.add("12月");

        BaseCriteria criteria1 = DateUtil.getDateSearchCriteria(month1Start, month1End, "time");
        BaseCriteria criteria2 = DateUtil.getDateSearchCriteria(month2Start, month2End, "time");
        BaseCriteria criteria3 = DateUtil.getDateSearchCriteria(month3Start, month3End, "time");
        BaseCriteria criteria4 = DateUtil.getDateSearchCriteria(month4Start, month4End, "time");
        BaseCriteria criteria5 = DateUtil.getDateSearchCriteria(month5Start, month5End, "time");
        BaseCriteria criteria6 = DateUtil.getDateSearchCriteria(month6Start, month6End, "time");
        BaseCriteria criteria7 = DateUtil.getDateSearchCriteria(month7Start, month7End, "time");
        BaseCriteria criteria8 = DateUtil.getDateSearchCriteria(month8Start, month8End, "time");
        BaseCriteria criteria9 = DateUtil.getDateSearchCriteria(month9Start, month9End, "time");
        BaseCriteria criteria10 = DateUtil.getDateSearchCriteria(month10Start, month10End, "time");
        BaseCriteria criteria11 = DateUtil.getDateSearchCriteria(month11Start, month11End, "time");
        BaseCriteria criteria12 = DateUtil.getDateSearchCriteria(month12Start, month12End, "time");

        long count1 = peopleOpinionRepository.count(new Query(Criteria.where("is_del").is(0).andOperator(criteria1)));
        long count2 = peopleOpinionRepository.count(new Query(Criteria.where("is_del").is(0).andOperator(criteria2)));
        long count3 = peopleOpinionRepository.count(new Query(Criteria.where("is_del").is(0).andOperator(criteria3)));
        long count4 = peopleOpinionRepository.count(new Query(Criteria.where("is_del").is(0).andOperator(criteria4)));
        long count5 = peopleOpinionRepository.count(new Query(Criteria.where("is_del").is(0).andOperator(criteria5)));
        long count6 = peopleOpinionRepository.count(new Query(Criteria.where("is_del").is(0).andOperator(criteria6)));
        long count7 = peopleOpinionRepository.count(new Query(Criteria.where("is_del").is(0).andOperator(criteria7)));
        long count8 = peopleOpinionRepository.count(new Query(Criteria.where("is_del").is(0).andOperator(criteria8)));
        long count9 = peopleOpinionRepository.count(new Query(Criteria.where("is_del").is(0).andOperator(criteria9)));
        long count10 = peopleOpinionRepository.count(new Query(Criteria.where("is_del").is(0).andOperator(criteria10)));
        long count11 = peopleOpinionRepository.count(new Query(Criteria.where("is_del").is(0).andOperator(criteria11)));
        long count12 = peopleOpinionRepository.count(new Query(Criteria.where("is_del").is(0).andOperator(criteria12)));
        countList.add(count1);
        countList.add(count2);
        countList.add(count3);
        countList.add(count4);
        countList.add(count5);
        countList.add(count6);
        countList.add(count7);
        countList.add(count8);
        countList.add(count9);
        countList.add(count10);
        countList.add(count11);
        countList.add(count12);
//        long totalCount = count1 + count2+ count3+ count4+ count5+ count6+ count7+ count8+ count9+ count10+ count11+ count12;
//        map.put("totalCount",totalCount);
        map.put("countList", countList);
        map.put("dateList", dateList);
        return map;
    }


    public Map<String, Object> getOpinionCarCount(Integer type, String date) {
        Map<String, Object> map = new HashMap<>();
        if (type == null) {
            type = 1;
        }
        if (date == null || "".equals(date)) {
            date = DateUtil.getSysYear();
        }
        String startDate = DateUtil.getStartDate(type, date);
        String endDate = DateUtil.getEndDate(type, date);
        BaseCriteria criteria = DateUtil.getDateSearchCriteria(startDate, endDate, "addTime");
        Criteria criteria2 = BaseCriteria.where("is_del").is(0);
        long opinionCarCount = opinionCarRepository.count(new Query().addCriteria(criteria).addCriteria(criteria2));
        map.put("opinionCarCount", opinionCarCount);
        return map;
    }

    public PersonCountVo personCount(String streeCode, String villageCode) {
        PersonCountVo personCountVo = new PersonCountVo();
        // 总人数
        long all = 0;
        // 查询所有街道
        List<TownInfo> townInfos = townInfoRepository.findAll();
        List<PersonList> personLists = new ArrayList<>();
        if (townInfos != null && townInfos.size() > 0) {
            for (TownInfo townInfo : townInfos) {
                PersonList personList = new PersonList();
                // 根据townId 查询人员信息
                Query query = new Query(
                        Criteria.where("is_del").is(0)).
                        addCriteria(Criteria.where("townTypeId").is(townInfo.getId()));

                if (streeCode != null && !"".equals(streeCode)) {
                    query.addCriteria(Criteria.where("streetCode").is(streeCode));
                }
                if (villageCode != null && !"".equals(villageCode)) {
                    query.addCriteria(Criteria.where("villageCode").is(villageCode));
                }
                long num = personnelRepository.count(query);
                personList.setValue((int) num);
                personList.setName(townInfo.getName());
                personLists.add(personList);
                all += num;
            }
        }
        personCountVo.setPersonList(personLists);
        personCountVo.setTotalCount((int) all);
        return personCountVo;
    }

    public List<MonthCountVO> actionCenterCount(String streetCode, String villageCode) {
        ArrayList<MonthCountVO> resultList = new ArrayList<>();
        //当前时间
        LocalDateTime now = LocalDateTime.now();
        //当年的第一天
        LocalDateTime januaryTime = now.with(TemporalAdjusters.firstDayOfYear());
        for (int i = 0; i < 12; i++) {
            LocalDateTime time = januaryTime.plusMonths(i);
            //查询每月活动数量
            Query query = new Query().addCriteria(
                    Criteria.where("time").gte(DateSwitcher.getMinTime(time)).
                            andOperator(Criteria.where("time").lte(DateSwitcher.getMaxTime(time.with(TemporalAdjusters.lastDayOfMonth())))).
                            and("type").is(2).and("is_del").is(0)
            );
            if (streetCode != null && !"".equals(streetCode)) {
                query.addCriteria(Criteria.where("streetCode").is(streetCode));
            }
            if (villageCode != null && !"".equals(villageCode)) {
                query.addCriteria(Criteria.where("villageCode").is(villageCode));
            }
            long count = actionCenterRepository.count(query);
            MonthCountVO monthCountVO = new MonthCountVO();
            monthCountVO.setMonth(time.getMonthValue());
            monthCountVO.setCount(count);
            resultList.add(monthCountVO);
        }
        return resultList;
    }

    public List<ActionCenterDetailVO> actionCenterDetail(String streetCode, String villageCode) {
        ArrayList<ActionCenterDetailVO> resultList = new ArrayList<>();
        //获取今年的开始时间和结束时间
        LocalDateTime now = LocalDateTime.now();
        int year = now.getYear();
        LocalDateTime startTime = LocalDateTime.of(year, 1, 1, 00, 00, 00);
        LocalDateTime endTime = LocalDateTime.of(year, 12, 31, 23, 59, 59);
        //查询今年所有的活动
        Query query = new Query().addCriteria(
                Criteria.where("time").gte(startTime).
                        andOperator(Criteria.where("time").lte(endTime)).
                        and("type").is(2).and("is_del").is(0)).
                with(Sort.by(Sort.Direction.ASC, "time"));
        if (streetCode != null && !"".equals(streetCode)) {
            query.addCriteria(Criteria.where("streetCode").is(streetCode));
        }
        if (villageCode != null && !"".equals(villageCode)) {
            query.addCriteria(Criteria.where("villageCode").is(villageCode));
        }

        List<ActionCenter> actionCenterList = actionCenterRepository.findAll(query);
        actionCenterList.forEach(actionCenter -> {
            ActionCenterDetailVO actionCenterDetailVO = new ActionCenterDetailVO();
            actionCenterDetailVO.setName(actionCenter.getName());
            actionCenterDetailVO.setAddress(actionCenter.getAction_address());
            Date time = actionCenter.getTime();
            actionCenterDetailVO.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time));
            //获取活动类型
            Integer actionTypeId = actionCenter.getActionTypeId();
            switch (actionTypeId) {//活动类型(1:参政议政、2:民主监督、3:考察调研、4:会议、5:社会服务、6:其他)
                case 1:
                    actionCenterDetailVO.setActionTypeName("参政议政");
                    break;
                case 2:
                    actionCenterDetailVO.setActionTypeName("民主监督");
                    break;
                case 3:
                    actionCenterDetailVO.setActionTypeName("考察调研");
                    break;
                case 4:
                    actionCenterDetailVO.setActionTypeName("会议");
                    break;
                case 5:
                    actionCenterDetailVO.setActionTypeName("社会服务");
                    break;
                case 6:
                    actionCenterDetailVO.setActionTypeName("其他");
                    break;

            }
            resultList.add(actionCenterDetailVO);
        });
        return resultList;
    }


    public List<MonthCountVO> companyCount(String streetCode, String villageCode) {
        ArrayList<MonthCountVO> resultList = new ArrayList<>();
        //当前时间
        LocalDateTime now = LocalDateTime.now();
        //当年的第一天
        LocalDateTime januaryTime = now.with(TemporalAdjusters.firstDayOfYear());
        for (int i = 0; i < 12; i++) {
            LocalDateTime time = januaryTime.plusMonths(i);
            //查询每月活动数量
            Query query = new Query().
                    addCriteria(
                            Criteria.where("addTime").gte(DateSwitcher.getMinTime(time)).
                                    andOperator(Criteria.where("addTime").lte(DateSwitcher.getMaxTime(time.with(TemporalAdjusters.lastDayOfMonth())))).
                                    and("is_del").is(0)
                    );
            if (streetCode != null && !"".equals(streetCode)) {
                query.addCriteria(Criteria.where("streetCode").is(streetCode));
            }
            if (villageCode != null && !"".equals(villageCode)) {
                query.addCriteria(Criteria.where("villageCode").is(villageCode));
            }
            long count = overseasCompanyRepository.count(query);
            MonthCountVO monthCountVO = new MonthCountVO();
            monthCountVO.setMonth(time.getMonthValue());
            monthCountVO.setCount(count);
            resultList.add(monthCountVO);
        }
        return resultList;
    }


    public List<CompanyDetailVo> companyDetail(String streetCode, String villageCode) {
        ArrayList<CompanyDetailVo> resultList = new ArrayList<>();
        //获取今年的开始时间和结束时间
        LocalDateTime now = LocalDateTime.now();
        int year = now.getYear();
        LocalDateTime startTime = LocalDateTime.of(year, 1, 1, 00, 00, 00);
        LocalDateTime endTime = LocalDateTime.of(year, 12, 31, 23, 59, 59);
        //查询今年所有的活动
        Query query = new Query().addCriteria(
                Criteria.where("addTime").gte(startTime).
                        andOperator(Criteria.where("addTime").lte(endTime)).
                        and("is_del").is(0)).
                with(Sort.by(Sort.Direction.ASC, "addTime"));
        if (streetCode != null && !"".equals(streetCode)) {
            query.addCriteria(Criteria.where("streetCode").is(streetCode));
        }
        if (villageCode != null && !"".equals(villageCode)) {
            query.addCriteria(Criteria.where("villageCode").is(villageCode));
        }
        List<OverseasCompany> overseasCompanyList = overseasCompanyRepository.findAll(query);
        overseasCompanyList.forEach(company -> {
            CompanyDetailVo companyDetailVo = new CompanyDetailVo();
            companyDetailVo.setName(company.getName());
            companyDetailVo.setIntroduction(company.getIntroduction());
            companyDetailVo.setLegal(company.getLegal());
            companyDetailVo.setRegisterCapital(company.getRegisterCapital().toString());
            resultList.add(companyDetailVo);
        });
        return resultList;
    }

    public List<MonthCountVO> opinionCarCount(String streetCode, String villageCode) {
        ArrayList<MonthCountVO> resultList = new ArrayList<>();
        //当前时间
        LocalDateTime now = LocalDateTime.now();
        //当年的第一天
        LocalDateTime januaryTime = now.with(TemporalAdjusters.firstDayOfYear());
        for (int i = 0; i < 12; i++) {
            LocalDateTime time = januaryTime.plusMonths(i);
            //查询每月活动数量
            Query query = new Query().
                    addCriteria(
                            Criteria.where("time").gte(DateSwitcher.getMinTime(time)).
                                    andOperator(Criteria.where("time").lte(DateSwitcher.getMaxTime(time.with(TemporalAdjusters.lastDayOfMonth())))).
                                    and("is_del").is(0)
                    );
            if (streetCode != null && !"".equals(streetCode)) {
                query.addCriteria(Criteria.where("streetCode").is(streetCode));
            }
            if (villageCode != null && !"".equals(villageCode)) {
                query.addCriteria(Criteria.where("villageCode").is(villageCode));
            }
            long count = opinionCarRepository.count(query);
            MonthCountVO monthCountVO = new MonthCountVO();
            monthCountVO.setMonth(time.getMonthValue());
            monthCountVO.setCount(count);
            resultList.add(monthCountVO);
        }
        return resultList;
    }

    public List<OpinionCarDetailVo> opinionCarDetail(String streetCode, String villageCode) {
        ArrayList<OpinionCarDetailVo> resultList = new ArrayList<>();
        //获取今年的开始时间和结束时间
        LocalDateTime now = LocalDateTime.now();
        int year = now.getYear();
        LocalDateTime startTime = LocalDateTime.of(year, 1, 1, 00, 00, 00);
        LocalDateTime endTime = LocalDateTime.of(year, 12, 31, 23, 59, 59);
        //查询今年所有的活动
        Query query = new Query().addCriteria(
                Criteria.where("time").gte(startTime).
                        andOperator(Criteria.where("time").lte(endTime)).
                        and("is_del").is(0)).
                with(Sort.by(Sort.Direction.ASC, "time"));
        if (streetCode != null && !"".equals(streetCode)) {
            query.addCriteria(Criteria.where("streetCode").is(streetCode));
        }
        if (villageCode != null && !"".equals(villageCode)) {
            query.addCriteria(Criteria.where("villageCode").is(villageCode));
        }
        List<OpinionCar> opinionCarList = opinionCarRepository.findAll(query);
        opinionCarList.forEach(opinionCar -> {
            OpinionCarDetailVo opinionCarDetailVo = new OpinionCarDetailVo();
            opinionCarDetailVo.setTitle(opinionCar.getTitle());
            opinionCarDetailVo.setContent(opinionCar.getContent());
            opinionCarDetailVo.setAddUserName(opinionCar.getAddUserName());
            opinionCarDetailVo.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(opinionCar.getTime()));
            resultList.add(opinionCarDetailVo);
        });
        return resultList;
    }

    public List<BrandMapListVo> brandMapList(Integer type) {

        List<BrandMapListVo> brandMapListVos = new ArrayList<>();

        Query query = new Query().addCriteria(
                Criteria.where("is_del").is(0).and("type").is(type)).
                with(Sort.by(Sort.Direction.ASC, "time"));
        List<BasicDemo> basicDemoList = basicDemoRepository.findAll(query);
        basicDemoList.forEach(basicDemo -> {
            BrandMapListVo brandMapListVo = new BrandMapListVo();
            brandMapListVo.setName(basicDemo.getName());
            brandMapListVo.setInfo(basicDemo.getIntroduction());
            brandMapListVo.setLatitude((basicDemo.getLatitude() == null ? "" : basicDemo.getLatitude()));
            brandMapListVo.setLongtitue((basicDemo.getLongitude() == null ? "" : basicDemo.getLongitude()));
            brandMapListVo.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(basicDemo.getTime()));
            brandMapListVo.setUrl(basicDemo.getPicture());
            brandMapListVos.add(brandMapListVo);
        });
        return brandMapListVos;
    }
}
